//
//  YDYLiveTableViewCell.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/9.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYLiveTableViewCell.h"
#import "YDYLiveCollectionViewCell.h"
#import "YDYPartition.h"
#import "YDYLive.h"
#import "YDYLivePlayerController.h"
@interface YDYLiveTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end
static NSString * const ydyLiveCollectionviewCellIdentifier = @"ydyLiveCollectionviewCellIdentifier";

@implementation YDYLiveTableViewCell


#pragma mark -
#pragma mark -数据源 setter
-(void)setPartition:(YDYPartition *)partition{
    _partition = partition;
    
    [self.collectionView reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self createLayout];
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YDYLiveCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:ydyLiveCollectionviewCellIdentifier];

}
-(void)createLayout{
    CGFloat margin = 10;
    CGFloat width = (YDYScreenSize.width - margin * 3) / 2.0;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(width, 180);
    layout.minimumLineSpacing = margin;
    layout.minimumInteritemSpacing = margin;
    self.collectionView.collectionViewLayout = layout;
}
#
#pragma mark -
#pragma mark -collection delegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YDYLive *live = self.partition.lives[indexPath.item];
    [[NSNotificationCenter defaultCenter]postNotificationName:ydy_presentLivePlayerNotification object:live];
    
}


#pragma mark -
#pragma mark -collectionview datasource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
   YDYLiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ydyLiveCollectionviewCellIdentifier forIndexPath:indexPath];
    YDYLive *live = self.partition.lives[indexPath.item];
    cell.live = live;
    return cell;
    
}

@end
